package com.yehongyu.mytools.ui.applet;

import java.applet.Applet;
import java.awt.Button;
import java.awt.List;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class ListApplet extends Applet implements ActionListener,ItemListener{

	/**序列ID*/
	private static final long serialVersionUID = 1L;

	private String[] flavors = { "Chocolate", "Strawberry", "Vanilla Fudge Swirl",
			"Mint Chip", "Mocha Almond Fudge", "Rum Raisin", "Praline Cream",
			"Mud Pie" };
	int count = 0;
	
	TextArea t = new TextArea(flavors.length, 30);
	
	// Show 10 items, allow multiple selection:
	private List lst = new List(10, true);

	Button b = new Button("test");

	public void init() {
		setSize(400, 400);
		t.setEditable(false);
		for (int i = 0; i < 4; i++)
			lst.add(flavors[count++]);
		add(t);
		add(lst);
		add(b);
		lst.addItemListener(this);
		b.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(b)) {
			if (count < flavors.length)
				lst.add(flavors[count++], 0);
		}else{
			System.out.println("Unknown Action!");
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getSource().equals(lst)) {
			t.setText("");
			String[] items = lst.getSelectedItems();
			for (int i = 0; i < items.length; i++)
				t.append(items[i] + "\n");
		}else{
			System.out.println("Object is [" + e.getItem() + "],Unknown Action!");
		}
	}

}
